package com.chuangke.flow.util;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.collections.CollectionUtils;

import com.chuangke.common.exception.ChuangkeException;
import com.chuangke.flow.entity.FlowHiFlow;
import com.chuangke.flow.entity.FlowHiNode;
import com.chuangke.flow.entity.FlowHiNodeProcessor;
import com.chuangke.flow.entity.FlowHiTask;
import com.chuangke.flow.entity.FlowHiTaskLog;
import com.chuangke.flow.entity.FlowHiVariable;
import com.chuangke.flow.entity.FlowMdFlow;
import com.chuangke.flow.entity.FlowMdNode;
import com.chuangke.flow.entity.FlowMdNodeProcessor;
import com.chuangke.flow.entity.FlowRuFlow;
import com.chuangke.flow.entity.FlowRuNode;
import com.chuangke.flow.entity.FlowRuNodeProcessor;
import com.chuangke.flow.entity.FlowRuTask;
import com.chuangke.flow.entity.FlowRuVariable;

public class FlowModelUtils {

	public static FlowRuFlow convert(FlowMdFlow mdFlow) {
		if (mdFlow == null) {
			return null;
		}

		FlowRuFlow ruFlow = null;

		try {
			ruFlow = new FlowRuFlow();
			BeanUtils.copyProperties(ruFlow, mdFlow);
		} catch (Exception e) {
			throw new ChuangkeException(e);
		}

		if (CollectionUtils.isNotEmpty(mdFlow.getNodes())) {
			List<FlowRuNode> nodes = new ArrayList<>();
			mdFlow.getNodes().forEach(node -> nodes.add(convert(node)));
			ruFlow.setNodes(nodes);
		}

		return ruFlow;
	}

	public static FlowRuNode convert(FlowMdNode mdNode) {
		if (mdNode == null) {
			return null;
		}

		FlowRuNode ruNode = null;

		try {
			ruNode = new FlowRuNode();
			BeanUtils.copyProperties(ruNode, mdNode);
		} catch (Exception e) {
			throw new ChuangkeException(e);
		}

		if (CollectionUtils.isNotEmpty(mdNode.getProcessors())) {
			List<FlowRuNodeProcessor> processors = new ArrayList<>();
			mdNode.getProcessors().forEach(processor -> processors.add(convert(processor)));
			ruNode.setProcessors(processors);
		}

		return ruNode;
	}

	public static FlowRuNodeProcessor convert(FlowMdNodeProcessor mdNodeProcessor) {
		if (mdNodeProcessor == null) {
			return null;
		}

		FlowRuNodeProcessor ruNodeProcessor = null;

		try {
			ruNodeProcessor = new FlowRuNodeProcessor();
			BeanUtils.copyProperties(ruNodeProcessor, mdNodeProcessor);
		} catch (Exception e) {
			throw new ChuangkeException(e);
		}

		return ruNodeProcessor;
	}

	public static FlowHiFlow convert(FlowRuFlow ruflow) {
		if (ruflow == null) {
			return null;
		}

		FlowHiFlow hiFlow = null;

		try {
			hiFlow = new FlowHiFlow();
			BeanUtils.copyProperties(hiFlow, ruflow);
		} catch (Exception e) {
			throw new ChuangkeException(e);
		}

		return hiFlow;
	}

	public static FlowHiFlow convertRH(FlowRuFlow ruflow) {
		if (ruflow == null) {
			return null;
		}

		FlowHiFlow hiFlow = null;

		try {
			hiFlow = new FlowHiFlow();
			BeanUtils.copyProperties(hiFlow, ruflow);
		} catch (Exception e) {
			throw new ChuangkeException(e);
		}

		return hiFlow;
	}

	public static FlowHiNode convertRH(FlowRuNode ruNode) {
		if (ruNode == null) {
			return null;
		}

		FlowHiNode hiFlow = null;

		try {
			hiFlow = new FlowHiNode();
			BeanUtils.copyProperties(hiFlow, ruNode);
		} catch (Exception e) {
			throw new ChuangkeException(e);
		}

		return hiFlow;
	}

	public static FlowHiNodeProcessor convertRH(FlowRuNodeProcessor ruProcessor) {
		if (ruProcessor == null) {
			return null;
		}

		FlowHiNodeProcessor hiProcessor = null;

		try {
			hiProcessor = new FlowHiNodeProcessor();
			BeanUtils.copyProperties(hiProcessor, ruProcessor);
		} catch (Exception e) {
			throw new ChuangkeException(e);
		}

		return hiProcessor;
	}

	public static FlowHiVariable convertRH(FlowRuVariable ruVariable) {
		if (ruVariable == null) {
			return null;
		}

		FlowHiVariable hiVariable = null;

		try {
			hiVariable = new FlowHiVariable();
			BeanUtils.copyProperties(hiVariable, ruVariable);
		} catch (Exception e) {
			throw new ChuangkeException(e);
		}

		return hiVariable;
	}
	
	public static FlowHiTask convertRH(FlowRuTask ruTask) {
		if (ruTask == null) {
			return null;
		}

		FlowHiTask hiTask = new FlowHiTask();

		try {
			hiTask = new FlowHiTask();
			BeanUtils.copyProperties(hiTask, ruTask);
		} catch (Exception e) {
			throw new ChuangkeException(e);
		}

		return hiTask;
	}

	public static FlowHiTaskLog convertRHL(FlowRuTask ruTask) {
		if (ruTask == null) {
			return null;
		}

		FlowHiTaskLog hiTask = new FlowHiTaskLog();

		try {
			hiTask = new FlowHiTaskLog();
			BeanUtils.copyProperties(hiTask, ruTask);
		} catch (Exception e) {
			throw new ChuangkeException(e);
		}

		return hiTask;
	}
}
